﻿Imports TodoMicroSolutionPackV1.dal.dto
Imports TodoMicroSolutionPackV1.productos.dal.dto
Imports TodoMicroSolutionPackV1.entidades.dal.dto
Imports TodoMicroSolutionPackV1.facturacion.dal.dto
Imports Iesi.Collections.Generic
Imports NHibernate
Imports NHibernate.Cfg
Imports NHibernate.Criterion

Module comunes

    Public Sub populateCombobox(ByRef constantes As IList, ByVal displayMember As String, ByVal valueMember As String, ByRef combo As ComboBox)
        combo.DisplayMember = displayMember
        combo.ValueMember = valueMember
        combo.DataSource = constantes
    End Sub
    Public Sub populateDepositosCombobox(ByVal product_id As Integer, ByRef combo As ComboBox)
        Dim miSesion As ISession = NhibernateSessionFactory.getSession
        Dim results As IList = miSesion.CreateQuery("select s.deposito_id, sum(s.cantidad) from stock s where s.producto.id=" & product_id & " group by s.deposito_id").List()

        combo.DataSource = Nothing
        For Each row In results
            If row(1) > 0 Then
                combo.Items.Add(New DictionaryEntry(row(0), row(0)))
            End If
        Next
        combo.DisplayMember = "value"
        combo.ValueMember = "key"

        combo.DataSource = combo.Items
        miSesion.Close()
    End Sub

    Public Function getCliente(ByRef session As ISession) As entidad

        Return frmEntidadSelect.getCliente(session)
    End Function
    Public Function getProducto(ByRef session As ISession) As producto
        Return frmProductoSelect.getProducto(session)
    End Function
    Public Function getComprobante(ByRef session As ISession) As comprobante
        Return frmComprobanteSelect.getComprobante(session)
    End Function
    Public Function getConstante(ByRef session As ISession) As constante
        Return frmConstanteSelect.getConstante(session)
    End Function

    Public Function barCodeDigitoVerificador(ByVal str As String) As Char
        'etapa 1
        Dim dig As Integer = 0
        For i = 0 To str.Length - 1 Step 2
            dig = dig + Val(str(i))
        Next

        'etapa 2
        dig = dig * 3

        'etapa 3
        Dim dig2 As Integer = 0
        For i = 1 To str.Length - 1 Step 2
            dig2 = dig2 + Val(str(i))
        Next

        'etapa 4
        dig = dig + dig2

        'etapa 5
        Return (10 - (dig Mod 10)).ToString(0)
    End Function
    Public Function convertTo2of5BarCode(ByVal str As String) As String
        Dim dic As New Dictionary(Of Char, String)

        dic.Add("0", "00110")
        dic.Add("1", "10001")
        dic.Add("2", "01001")
        dic.Add("3", "11000")
        dic.Add("4", "00101")
        dic.Add("5", "10100")
        dic.Add("6", "01100")
        dic.Add("7", "00011")
        dic.Add("8", "10010")
        dic.Add("9", "01010")
        dic.Add("A", "00")
        dic.Add("Z", "10")

        If str.Length Mod 2 <> 0 Then
            str = "0" + str
        End If
        str = "AA" + str + "ZA"

        Dim seq As String = ""
        For i = 0 To str.Length - 1 Step 2
            Dim char_bar As Char = str(i)
            Dim char_space As Char = str(i + 1)

            If dic(char_bar) Is Nothing OrElse dic(char_space) Is Nothing Then
                Throw New ApplicationException("Caracter invalido para codigo de barras I25: " + str)
            End If

            For k = 0 To dic(char_bar).Length - 1
                seq = seq + dic(char_bar)(k) + dic(char_space)(k)
            Next
        Next
        Dim seq2 As String = ""
        For i = 0 To Len(seq) - 1
            Dim c As Char = IIf(i Mod 2 = 0, "n", "b")
            seq2 = seq2 + IIf(seq(i) = "0", c, Char.ToUpper(c))
        Next

        Return seq2
    End Function
    Public Function Num2Text(ByVal value As Double) As String
        Select Case value
            Case 0 : Num2Text = "CERO"
            Case 1 : Num2Text = "UN"
            Case 2 : Num2Text = "DOS"
            Case 3 : Num2Text = "TRES"
            Case 4 : Num2Text = "CUATRO"
            Case 5 : Num2Text = "CINCO"
            Case 6 : Num2Text = "SEIS"
            Case 7 : Num2Text = "SIETE"
            Case 8 : Num2Text = "OCHO"
            Case 9 : Num2Text = "NUEVE"
            Case 10 : Num2Text = "DIEZ"
            Case 11 : Num2Text = "ONCE"
            Case 12 : Num2Text = "DOCE"
            Case 13 : Num2Text = "TRECE"
            Case 14 : Num2Text = "CATORCE"
            Case 15 : Num2Text = "QUINCE"
            Case Is < 20 : Num2Text = "DIECI" & Num2Text(value - 10)
            Case 20 : Num2Text = "VEINTE"
            Case Is < 30 : Num2Text = "VEINTI" & Num2Text(value - 20)
            Case 30 : Num2Text = "TREINTA"
            Case 40 : Num2Text = "CUARENTA"
            Case 50 : Num2Text = "CINCUENTA"
            Case 60 : Num2Text = "SESENTA"
            Case 70 : Num2Text = "SETENTA"
            Case 80 : Num2Text = "OCHENTA"
            Case 90 : Num2Text = "NOVENTA"
            Case Is < 100 : Num2Text = Num2Text(Int(value \ 10) * 10) & " Y " & Num2Text(value Mod 10)
            Case 100 : Num2Text = "CIEN"
            Case Is < 200 : Num2Text = "CIENTO " & Num2Text(value - 100)
            Case 200, 300, 400, 600, 800 : Num2Text = Num2Text(Int(value \ 100)) & "CIENTOS"
            Case 500 : Num2Text = "QUINIENTOS"
            Case 700 : Num2Text = "SETECIENTOS"
            Case 900 : Num2Text = "NOVECIENTOS"
            Case Is < 1000 : Num2Text = Num2Text(Int(value / 100) * 100) & " " & Num2Text(value Mod 100)
            Case Is < 2000 : Num2Text = "MIL " & Num2Text(value Mod 1000)
            Case Is < 1000000 : Num2Text = Num2Text(Int(value \ 1000)) & " MIL"
                If value Mod 1000 Then Num2Text = Num2Text & " " & Num2Text(value Mod 1000)
            Case 1000000 : Num2Text = "UN MILLON"
            Case Is < 2000000 : Num2Text = "UN MILLON " & Num2Text(value Mod 1000000)
            Case Is < 1000000000000.0# : Num2Text = Num2Text(Int(value / 1000000)) & " MILLONES "
                If (value - Int(value / 1000000) * 1000000) Then Num2Text = Num2Text & " " & Num2Text(value - Int(value / 1000000) * 1000000)
            Case 1000000000000.0# : Num2Text = "UN BILLON"
            Case Is < 2000000000000.0# : Num2Text = "UN BILLON " & Num2Text(value - Int(value / 1000000000000.0#) * 1000000000000.0#)
            Case Else : Num2Text = Num2Text(Int(value / 1000000000000.0#)) & " BILLONES"
                If (value - Int(value / 1000000000000.0#) * 1000000000000.0#) Then Num2Text = Num2Text & " " & Num2Text(value - Int(value / 1000000000000.0#) * 1000000000000.0#)
        End Select


    End Function
End Module
